package com.mark.common;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.mark.web.data.enums.ResponseCode;

import java.io.Serializable;

/**
 * @Description: 返回前端的对象模型
 * @Author: Mark
 * @CreateDate: Mark on 2018/3/22.
 * @Version: 2.0
 * @Copyright : 豆浆油条个人非正式工作室
 * <p>
 * 注：@JsonInclude(JsonInclude.Include.NON_NULL) ：实现的效果当序列化Json对象时，如果是null的对象，key也会消失
 */
@JsonInclude(JsonInclude.Include.NON_NULL)
public class ServerResponse<T> implements Serializable, ServerResBase {

    /**
     * 响应状态码
     */
    private final int status;

    /**
     * 响应的提示信息
     */
    private String msg;

    /**
     * 响应数据
     */
    private T data;


    private ServerResponse(int status) {
        this.status = status;
    }

    private ServerResponse(int status, T data) {
        this.status = status;
        this.data = data;
    }

    private ServerResponse(int status, String msg, T data) {
        this.status = status;
        this.msg = msg;
        this.data = data;
    }

    private ServerResponse(int status, String msg) {
        this.status = status;
        this.msg = msg;
    }

    @JsonIgnore
    public boolean isSuccess() {
        return this.status == ResponseCode.SUCCESS.getCode();
    }

    public int getStatus() {
        return status;
    }

    public String getMsg() {
        return msg;
    }

    public T getData() {
        return data;
    }

    public static <T> ServerResponse<T> model(T data) {
        return new ServerResponse<T>(ResponseCode.SUCCESS.getCode(), data);
    }

    public static <T> ServerResponse<T> createBySuccess() {
        return new ServerResponse<T>(ResponseCode.SUCCESS.getCode(), ResponseCode.SUCCESS.getDesc());
    }

    public static <T> ServerResponse<T> createBySuccessMessage(String msg) {
        return new ServerResponse<T>(ResponseCode.SUCCESS.getCode(), msg);
    }

    public static <T> ServerResponse<T> createBySuccess(T data) {
        return new ServerResponse<T>(ResponseCode.SUCCESS.getCode(), data);
    }

    public static <T> ServerResponse<T> createBySuccess(String msg, T data) {
        return new ServerResponse<T>(ResponseCode.SUCCESS.getCode(), msg, data);
    }

    public static <T> ServerResponse<T> createByError() {
        return new ServerResponse<T>(ResponseCode.ERROR.getCode(), ResponseCode.ERROR.getDesc());
    }

    public static <T> ServerResponse<T> createByErrorMessage(String errorMessage) {
        return new ServerResponse<T>(ResponseCode.ERROR.getCode(), errorMessage);
    }

    /**
     * 参数有误的返回格式
     *
     * @param errorMessage ：提示信息
     * @param <T>
     * @return
     */
    public static <T> ServerResponse<T> createByIllegalArgument(String errorMessage) {
        return new ServerResponse<T>(ResponseCode.ILLEGAL_ARGUMENT.getCode(), errorMessage);
    }

    public static <T> ServerResponse<T> createByException(String errorMessage) {
        return new ServerResponse<T>(ResponseCode.Exception.getCode(), errorMessage);
    }

    public static <T> ServerResponse<T> createByNeedLogin() {
        return new ServerResponse<T>(ResponseCode.NEED_LOGIN.getCode(), ResponseCode.NEED_LOGIN.getDesc());
    }

    public static <T> ServerResponse<T> createBySellerNeedLogin() {
        return new ServerResponse<T>(ResponseCode.SELLER_NEED_LOGIN.getCode(), ResponseCode.NEED_LOGIN.getDesc());
    }

    public static <T> ServerResponse<T> createByErrorCodeMessage(int errorCode, String errorMessage) {
        return new ServerResponse<T>(errorCode, errorMessage);
    }

}
